// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.query.sections;

import java.util.Map;

import org.dengues.core.DenguesCorePlugin;
import org.dengues.designer.ui.query.AbstractFormPage;
import org.dengues.model.DenguesModelManager;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.model.database.SectionUnions;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-12-8 qiang.zhang $
 * 
 */
public class UnionsSection extends TableSectionPart {

    /**
     * Qiang.Zhang.Adolf@gmail.com ColumnsSection constructor comment.
     * 
     * @param parent
     * @param toolkit
     * @param style
     */
    public UnionsSection(AbstractFormPage page, Composite parent) {
        super(page, parent, new String[] {}, SWT.CHECK);
        getSection().setText("Union");
        getSection().setDescription(
                "Specify union query to combine specified records from more than one query into a result set. ");
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.query.sections.TableSectionPart#createTableColumns()
     */
    @Override
    public void createTableColumns(final TableViewer viewerPart) {
        final Table table = viewerPart.getTable();
        TableColumn column = new TableColumn(table, SWT.NONE);
        column.setText("Queries");
        column.pack();
        column = new TableColumn(table, SWT.NONE);
        column.setText("Union");
        column.pack();
        column = new TableColumn(table, SWT.NONE);
        column.setText("Comment");
        column.pack();
        ViewerPartProvider provider = new ViewerPartProvider(TYPE_UNIONS);
        viewerPart.setContentProvider(provider);
        viewerPart.setLabelProvider(provider);
        Map<String, Object> existNames = getExistNames(TYPE_UNIONS);
        Map<String, DatabaseDiagram> warehouseQueries = DenguesCorePlugin.getDefault().getWarehousePluginService()
                .getWarehouseViewFactory().getWarehouseQueries();
        for (DatabaseDiagram databaseDiagram : warehouseQueries.values()) {
            if (!dbDiagram.getName().equals(databaseDiagram.getName())) {
                if (existNames.keySet().contains(databaseDiagram.getName())) {
                    SectionUnions object = (SectionUnions) existNames.get(databaseDiagram.getName());
                    object.setQueries(databaseDiagram.getWholeSql());
                } else {
                    SectionUnions sectionUnions = DenguesModelManager.getDatabaseFactory().createSectionUnions();
                    sectionUnions.setName(databaseDiagram.getName());
                    sectionUnions.setUnion(UNIONS.get(0));
                    sectionUnions.setQueries(databaseDiagram.getWholeSql());
                    dbDiagram.getSectionUnions().add(sectionUnions);
                }
            }
        }
        viewerPart.setInput(dbDiagram);
        TableItem[] selection2 = (viewerPart).getTable().getItems();
        for (TableItem tableItem : selection2) {
            tableItem.setChecked(((SectionUnions) tableItem.getData()).isChecked());
        }
        handleCCombo(viewerPart, table, 1, TYPE_UNIONS);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.query.sections.TableSectionPart#selectionChanged(org.eclipse.jface.viewers.IStructuredSelection)
     */
    @Override
    protected void selectionChanged(IStructuredSelection selection) {
        TableItem[] selection2 = ((TableViewer) viewerPart).getTable().getItems();
        for (TableItem tableItem : selection2) {
            ((SectionUnions) tableItem.getData()).setChecked(tableItem.getChecked());
            markDirty();
        }
    }
}
